/**
 * HOME
 */
Divo.app.home = function() {
	/* ----------------------- private属性 ----------------------- */
	var tn = 1;
	var fldLoginName, fldPassword;
	var oStatusBar;

	/* ----------------------- private方法 ----------------------- */
	// 创建表单
	function createForm() {
		fldLoginName = new Ext.form.TextField( {
			tabIndex : tn++,
			applyTo : 'login-username'
		});
		fldLoginName.on('specialkey', onEnter);

		fldPassword = new Ext.form.TextField( {
			tabIndex : tn++,
			inputType : 'password',
			applyTo : 'login-password'
		});
		fldPassword.on('specialkey', onEnter);
	}

	// 按回车键直接登录
	function onEnter(o, e) {
		if (e.getKey() === e.ENTER) {
			onSubmit();
			e.stopEvent();
		}
	}
	
	// 登录提交
	function onSubmit() {
		var name = fldLoginName.getValue();
		var pswd = fldPassword.getValue();
		
		var onLogin = function(retValue) {
			if (retValue) {
				//Divo.say("登录成功");
				window.location = Divo.getFullPath()+"app/sample/main.seam";
				return;
			}
			Divo.error("登录失败");
		}
		
		Divo.wait("正在登录...");
		Divo.rmtCallAsync("sampleFacade.login",onLogin,[name,pswd]);
	}

	// 注册新用户
	function onRegister() {
		window.location = Divo.getFullPath()+"app/register.seam";
	}
	
	//数据库初始化
	function onDbInit() {
	    var onDbInited = function(retValue) {
	        Divo.say("数据库初始化完毕ok"); 
	    }
	    Divo.wait("正在数据库初始化...");
	    Divo.rmtCallAsync("sampleFacade.dbInit",onDbInited);
	}

	/* ----------------------- public方法 ----------------------- */
	return {

		//初始化�
		init : function() {
			oStatusBar = Divo.app.StatusBar;
			oStatusBar.init();

			Divo.rmtCallSynch("sampleFacade.logout",Ext.emptyFn);
			createForm();

			Ext.get('login-login').on('click', function() {
				onSubmit();
			});

			Ext.get('login-register').on('click', function() {
				onRegister();
			});

			var el = Ext.get('db-init');
			if (el)
			   el.on('click', function() {onDbInit(); });

			Divo.endLoading();
		}

	}; // return

}();

Ext.onReady(Divo.app.home.init, Divo.app.home, true);

// EOP

